package com.y5neko.RadiantKnightExploit.UI;

import com.y5neko.RadiantKnightExploit.proxy.ProxySetting;
import javafx.animation.FadeTransition;
import javafx.animation.Timeline;
import javafx.geometry.HPos;
import javafx.geometry.Insets;
import javafx.scene.Scene;
import javafx.scene.control.*;
import javafx.scene.layout.AnchorPane;
import javafx.scene.layout.GridPane;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;
import javafx.util.Duration;

public class ProxyStage extends Stage {
    /**
     * 创建代理设置窗口
     */
    public ProxyStage() {
        // 创建全局代理对象
        ProxySetting globalProxySetting = new ProxySetting();

        // 创建一个VBox和GridPane用于指定布局
        VBox vBox = new VBox();
        GridPane gp = new GridPane();
        gp.setPadding(new Insets(10,10,10,20));
        gp.setHgap(5);
        gp.setVgap(15);

        // 创建一个单选按钮组，用以判断代理设置类型
        ToggleGroup isProxyToggleGroup = new ToggleGroup();

        // 创建三个单选按钮
        RadioButton noProxy = new RadioButton("无代理");
        RadioButton httpProxy = new RadioButton("HTTP代理");
        RadioButton socksProxy = new RadioButton("SOCKS代理");

        // 先检查当前代理状态，再设置默认选择，主要用于同步展示当前代理状态
        String[] proxyInfo = globalProxySetting.getProxy();
        if (proxyInfo[0].equals("true")){
            if (proxyInfo[1].equals("HTTP")){
                httpProxy.setSelected(true);
            } else if (proxyInfo[1].equals("Socks")){
                socksProxy.setSelected(true);
            }
        } else if (proxyInfo[0].equals("false")) {
            noProxy.setSelected(true);
        }

        // 将三个单选按钮添加到单选按钮组
        noProxy.setToggleGroup(isProxyToggleGroup);
        httpProxy.setToggleGroup(isProxyToggleGroup);
        socksProxy.setToggleGroup(isProxyToggleGroup);

        // 创建两个输入框，用于传递代理地址
        Label proxyAddrLabel = new Label("代理IP:");
        TextField proxyAddr = new TextField(proxyInfo[2]);
        Label proxyPortLabel = new Label("代理端口:");
        TextField proxyPort = new TextField(proxyInfo[3]);

        // 设置输入框的列约束为2
        GridPane.setColumnSpan(proxyAddr, 2);
        GridPane.setColumnSpan(proxyPort, 2);

        // 创建保存和取消按钮
        Button saveProxy = new Button("保存");
        Button cancelProxy = new Button("取消");
        saveProxy.setStyle("-fx-background-color: #007bff; -fx-text-fill: white; -fx-font-size: 10pt;");
        cancelProxy.setStyle("-fx-background-color: rgba(255,0,0,0.53); -fx-text-fill: white; -fx-font-size: 10pt;");

        // 监听取消按钮
        cancelProxy.setOnAction(event -> {
            this.close();
        });

        // 监听保存按钮
        saveProxy.setOnAction(event -> {
            String ipPattern = "^((25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\\.){3}(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$";
            java.util.regex.Pattern pattern = java.util.regex.Pattern.compile(ipPattern);
            java.util.regex.Matcher matcher = pattern.matcher(proxyAddr.getText());
            if (!matcher.matches() || Integer.parseInt(proxyPort.getText()) >= 65536 || Integer.parseInt(proxyPort.getText()) <= 0) {
                Alert alert = new Alert(Alert.AlertType.ERROR);
                alert.setTitle("代理格式错误");
                alert.setHeaderText("代理地址不合法");
                alert.setContentText("请输入合法的代理地址");
                alert.showAndWait();
                return;
            }

            // 首先判断当前选择的单选按钮
            Toggle selectedToggle = isProxyToggleGroup.getSelectedToggle();
            if (selectedToggle == noProxy) {
                System.out.println("选择了无代理");
                globalProxySetting.setProxyStatus(false);
            } else if (selectedToggle == httpProxy) {
                System.out.println("选择了有代理");
                globalProxySetting.setProxyStatus(true);
                globalProxySetting.setProxyType("HTTP");
                globalProxySetting.setProxyHost(proxyAddr.getText());
                globalProxySetting.setProxyPort(Integer.parseInt(proxyPort.getText()));
            } else if (selectedToggle == socksProxy) {
                System.out.println("选择了有代理");
                globalProxySetting.setProxyStatus(true);
                globalProxySetting.setProxyType("Socks");
                globalProxySetting.setProxyHost(proxyAddr.getText());
                globalProxySetting.setProxyPort(Integer.parseInt(proxyPort.getText()));
            } else {
                System.out.println("未选择代理类型");
                globalProxySetting.setProxyStatus(false);
            }
            globalProxySetting.setProxy();
            Alert successAlert = new Alert(Alert.AlertType.INFORMATION);
            successAlert.setTitle("保存成功");
            successAlert.setHeaderText("代理设置保存成功");
            successAlert.showAndWait();
            this.close();
        });

        // Cancel按钮美化
        FadeTransition fadeTransition = new FadeTransition(Duration.seconds(0.5), cancelProxy);
        fadeTransition.setFromValue(1.0);
        fadeTransition.setToValue(0.0);
        fadeTransition.setCycleCount(Timeline.INDEFINITE);
        fadeTransition.setAutoReverse(true);
        fadeTransition.play();

        // 将所有的控件添加进网格视图
        gp.add(noProxy, 0, 1); gp.add(httpProxy, 1, 1); gp.add(socksProxy, 2, 1);
        gp.add(proxyAddrLabel, 0, 2); gp.add(proxyAddr, 1, 2);
        gp.add(proxyPortLabel, 0, 3); gp.add(proxyPort, 1, 3);
        gp.add(cancelProxy, 0, 4); gp.add(saveProxy, 2, 4);

        // 设置返回按钮为右居中
        GridPane.setHalignment(cancelProxy, HPos.RIGHT);

        // 添加四个锚点
        AnchorPane anchorPane = new AnchorPane();
        AnchorPane.setTopAnchor(gp, 10.0);
        AnchorPane.setLeftAnchor(gp, 10.0);
        AnchorPane.setRightAnchor(gp, 10.0);
        AnchorPane.setBottomAnchor(gp, 10.0);

        anchorPane.getChildren().add(gp);
        vBox.getChildren().add(anchorPane);

        Scene scene = new Scene(vBox);
        this.setScene(scene);
        this.setTitle("设置代理");
        this.getIcons().add(com.y5neko.RadiantKnightExploit.Main.icon);
        this.setResizable(false);
        this.show();
    }
}